require 'View.Base.BehaviourView'

LinkEffect = class("LinkEffect", BehaviourView)

function LinkEffect:Awake(obj)
    BehaviourView.Awake(self, obj)

    self.materials = {}
    self.scaleCache = {}
    local rendererList = self.transform:GetComponentsInChildren(typeof(UnityEngine.Renderer))
    for i=1,rendererList.Length do
        local renderer = rendererList[i-1]
        local particle = renderer.transform:GetComponent('ParticleSystem')
        if particle==nil or particle.main==nil or particle.main.scalingMode==0 then
            local materials = renderer.materials
            for j=1,materials.Length do
                local material = materials[j-1]
                table.insert(self.materials, material)
                table.insert(self.scaleCache,material.mainTextureScale)
            end
        end
    end

    self:OnShow()
end
function LinkEffect:OnShow()
    BehaviourView.OnShow(self)
    self:Reset(self.transform.position)
end
function LinkEffect:Reset(position)
    self.startPos = position or self.transform.position
    self.lastScale = -1
    --print("LinkEffect:Reset!!!",self, self.gameObject.name, self.startPos.x, self.startPos.y, self.startPos.z)
    self:SetScale(0)
end
function LinkEffect:LateUpdate()
    local scale = Vector3.Distance(self.startPos, self.transform.position)/2
    --print("LinkEffect:LateUpdate!!!",self,self.transform.position.x, self.transform.position.y, self.transform.position.z, scale)
    if self.lastScale~=scale then
        self:SetScale(scale)
        self.lastScale = scale
    end
end


function LinkEffect:SetScale(nScale)
    self.transform.localScale = Vector3.New(1 , 1, nScale)
    --do return end
    for i,material in ipairs(self.materials) do
        local scale = self.scaleCache[i]
        material.mainTextureScale = Vector2(scale.x*nScale, scale.y)
    end
end